package com.dmap.admin.controller.backgroundv2;

import com.dmap.admin.annotation.LimitRequest;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.service.backgroundv2.IdentifyingCodeService;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * \* User: helei
 * \* Date: 18/05/18
 * \* Description:
 * \
 */
@Api(value = "验证码获取接口", description = "个人数据服务系统/验证码获取接口")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v2/identifying/")
public class IdentifyingCodeController {

    private static final String LANG = "language";

    @Resource
    private IdentifyingCodeService identifyingCodeService;

    @LimitRequest(limitCounts = 1, timeSecond = 30)
    @ResponseBody
    @ApiOperation(value = "获取短信验证码",
            notes = "1. 必填： 手机号， 国家码， 语言编号； " +
                    "2. 返回成功， 不再返回验证码； ")
    @ApiImplicitParams(@ApiImplicitParam(name = "language", value = "language", required = true, dataType = "String", paramType = "header"))
    @RequestMapping(value = "/phone/code/phone", method = RequestMethod.GET)
    public BaseVo sendPhoneCode(@RequestParam(value = "phone") String phone,
                                @RequestParam(value = "nationCode") String nationCode,
                                HttpServletRequest request) {
        BaseVo baseVo = new BaseVo();
        String language = request.getHeader(LANG);
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(nationCode) || StringUtils.isEmpty(language)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        String code = identifyingCodeService.sendIdentifyingCode(phone, language, nationCode);
        if (StringUtils.isBlank(code)) {
            baseVo.setCode(AdminConstant.RESPONSE_CODE_SEND_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_CODE_SEND_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "验证短信验证码", notes = "1. 必填： 手机号， 验证码； ")
    @RequestMapping(value = "/phone/code/validatePhoneCode", method = RequestMethod.GET)
    public BaseVo validatePhoneCode(@RequestParam(value = "phone") String phone,
                                    @RequestParam(value = "code") String code) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(code)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        String value = identifyingCodeService.getIdentifyingCode(phone);
        if (StringUtils.isBlank(value)) {
            baseVo.setCode(AdminConstant.RESPONSE_CODE_VALIDATE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_CODE_VALIDATE_ERROR_MSG);
            return baseVo;
        }
        if (!value.equals(code)) {
            baseVo.setCode(AdminConstant.RESPONSE_CODE_WRONG_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_CODE_WRONG_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

}